define(window.backboneCommon.getViewDefine("targets.appAction"), function ($,require,Collection,tpl) {
    var defaultsOptions={
        btnSubmitText:"确定",
        checkEmpty:false,
        submitCallback:null
    };
    var pagePrivate = {
        getDefaults :function () {
            return defaultsOptions
        },
        getOptions : function (options) {
            options=$.ExtCommon.isCheckEmpty(options)?{}:options;
            return $.ExtCommon.initMergeJson(this.getDefaults(),options);
        }
    };
    var backboneView= Backbone.View.extend({
        events: {
            "hidden.bs.modal #modal_targetAppCategory": "hideModalCallback",
            "click #submit_modal_targetAppAction": "saveSubmit"
        },
        initialize: function () {
            this.listenTo(this.model, "bind:AppAction", this.bindAppAction);
            this.listenTo(this.model, "bind:UserAction", this.bindUserAction);
            this.render();
        },
        render:function(){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml());
            return this;
        },
        initMain:function(options){
            this.elementModel={
                category:$("#panel_modal_TargetAppCategory"),
                userAction:$("#panel_modal_TargetAppUserAction"),
                formDay:$("#txt_modal_TargetApp_FormDay"),
                form:$("#form_modal_TargetApp"),
                btnSubmit:$("#submit_modal_targetAppAction"),
                modal:$("#modal_targetAppAction")
            };
            this.extObject={};
            this.pageData={initPage:false};
            this.pageOptions=$.ExtCommon.initMergeJson(pagePrivate.getOptions(),options);
            this.elementModel.btnSubmit.text(this.pageOptions.btnSubmitText);
        },
        initPage:function (json) {
            this.pageData.listSelect=json||{};
            var arrUserAction= $.ExtCommon.isCheckEmpty(this.pageData.listSelect.action)
                ?[]:
                $.map(this.pageData.listSelect.action,function(info){return info.id.toString()});
            if(!this.pageData.initPage){
                this.pageData.initPage=true;
                this.collection.initAfterAppAction();
            }else{
                this._showModal();
                this.extObject.category.setSelected(this.pageData.listSelect.category);
                this.extObject.userAction.setValue(arrUserAction);
            }
            this.elementModel.formDay.val(this.pageData.listSelect.fromDay?this.pageData.listSelect.fromDay:"30");
            this.elementModel.form.ExtParsley();
        },
        bindAppAction:function () {
            var self=this;
            var res=self.model.get("dataAppAction");
            self.extObject.category=self.elementModel.category.ExtSelectedMove({
                attrName:"appAction",placeholder:"请输入分类名称",initValue:self.pageData.listSelect.category, data:res.data,
                checkMaxLength:10,isMergeParent:false,
                initCallback:function(){self._showModal();}
            });
        },
        bindUserAction:function(initValue) {
            var res=this.model.get("dataUserAction");
            this.extObject.userAction = this.elementModel.userAction.ExtRadioCheckbox({
                type: "checkbox", attrName: "userAction", defaultInfo: {width: 70},
                data: res.data, initValue: initValue
            });
        },
        _checkForm:function () {
            $.ExtNotify.destroy();
            var isExtCheck=true;
            var appCategorySelect=this.extObject.category.getSelectedId();
            if($.ExtCommon.isCheckEmpty(appCategorySelect)){
                isExtCheck=false;
                $.ExtNotify.error("请选择分类");
            }else{
                if(appCategorySelect.length>10){
                    isExtCheck=false;
                    $.ExtNotify.error("已选择分类超过最大数");
                }
            }
            if($.ExtCommon.isCheckEmpty(this.extObject.userAction.getValue())){
                isExtCheck=false;
                $.ExtNotify.error("请选择用户行为");
            }
            return this.elementModel.form.ExtParsley("validate") && isExtCheck;
        },
        saveSubmit:function () {
            var self=this;
            if(self._checkForm()){
                var jsonData={};
                var appCategory=self.extObject.category.getSelectedList();
                var userAction=self.extObject.userAction.getValueList();
                var formDay= $.trim(self.elementModel.formDay.val().removesplit());
                if(appCategory.length>0){jsonData.category=appCategory;}
                if(userAction.length>0){jsonData.action=userAction;}
                if(formDay.length>0){jsonData.fromDay=formDay;}
                self._hideModal();
                if(self.pageOptions.checkEmpty && $.ExtCommon.isCheckEmpty(jsonData)){
                    $.ExtSweetAlert.confirm({
                        confirmButtonText: "是",
                        cancelButtonText:"否",
                        title:"是否批量删除APP行为定向？",
                        infoExt:{
                            confirmCallback:function(cb){
                                if(self.pageOptions.submitCallback){
                                    self.pageOptions.submitCallback(jsonData,function(isSuccess){
                                        cb(true);
                                        if(!isSuccess){ self._showModal();}
                                    });
                                }
                            },
                            cancelCallback:function(){
                                self._showModal();
                            }
                        }
                    });
                }else{
                    if(self.pageOptions.submitCallback){
                        self.pageOptions.submitCallback(jsonData,function(isSuccess){
                            if(!isSuccess){self._showModal();}
                        });
                    }
                }
            }
        },
        _showModal:function(){
            this.elementModel.modal.modal("show");
            $.ExtAssist.modal.computeTop(this.elementModel.modal);
        },
        _hideModal: function () {
            this.elementModel.modal.modal("hide");
        }
    });
    var viewModel=function(options){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({el:options.pElement,collection:this.collection,model:this.model});
        this.view.initMain(options);
    };
    viewModel.prototype={
        init:function (json) {
            this.view.initPage(json);
        }
    };
    return viewModel;
});